package cn.hahah.module.blog.service.usercollect;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.usercollect.vo.*;
import cn.hahah.module.blog.dal.dataobject.usercollect.UserCollectDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.usercollect.UserCollectMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户收藏 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserCollectServiceImpl implements UserCollectService {

    @Resource
    private UserCollectMapper userCollectMapper;

    @Override
    public Long createUserCollect(UserCollectSaveReqVO createReqVO) {
        // 插入
        UserCollectDO userCollect = BeanUtils.toBean(createReqVO, UserCollectDO.class);
        userCollectMapper.insert(userCollect);
        // 返回
        return userCollect.getId();
    }

    @Override
    public void updateUserCollect(UserCollectSaveReqVO updateReqVO) {
        // 校验存在
        validateUserCollectExists(updateReqVO.getId());
        // 更新
        UserCollectDO updateObj = BeanUtils.toBean(updateReqVO, UserCollectDO.class);
        userCollectMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserCollect(Long id) {
        // 校验存在
        validateUserCollectExists(id);
        // 删除
        userCollectMapper.deleteById(id);
    }

    private void validateUserCollectExists(Long id) {
        if (userCollectMapper.selectById(id) == null) {
            throw exception(USER_COLLECT_NOT_EXISTS);
        }
    }

    @Override
    public UserCollectDO getUserCollect(Long id) {
        return userCollectMapper.selectById(id);
    }

    @Override
    public PageResult<UserCollectDO> getUserCollectPage(UserCollectPageReqVO pageReqVO) {
        return userCollectMapper.selectPage(pageReqVO);
    }

}